home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Ebooks / Thinking in C++ V2 / C20 / TokenIteratorTest.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-25  |  2.0 KB  |  61 lines

  1. //: C20:TokenIteratorTest.cpp
  2. // From Thinking in C++, 2nd Edition
  3. // Available at http://www.BruceEckel.com
  4. // (c) Bruce Eckel 1999
  5. // Copyright notice in Copyright.txt
  6. #include "TokenIterator.h"
  7. #include "../require.h"
  8. #include <fstream>
  9. #include <iostream>
  10. #include <vector>
  11. #include <deque>
  12. #include <set>
  13. using namespace std;
  14.  
  15. int main() {
  16.   ifstream in("TokenIteratorTest.cpp");
  17.   assure(in, "TokenIteratorTest.cpp");
  18.   ostream_iterator<string> out(cout, "\n");
  19.   typedef istreambuf_iterator<char> IsbIt;
  20.   IsbIt begin(in), isbEnd;
  21.   Delimiters 
  22.     delimiters(" \t\n~;()\"<>:{}[]+-=&*#.,/\\");
  23.   TokenIterator<IsbIt, Delimiters> 
  24.     wordIter(begin, isbEnd, delimiters),
  25.     end;
  26.   vector<string> wordlist;
  27.   copy(wordIter, end, back_inserter(wordlist));
  28.   // Output results:
  29.   copy(wordlist.begin(), wordlist.end(), out);
  30.   *out++ = "-----------------------------------";
  31.   // Use a char array as the source:
  32.   char* cp = 
  33.     "typedef std::istreambuf_iterator<char> It";
  34.   TokenIterator<char*, Delimiters>
  35.     charIter(cp, cp + strlen(cp), delimiters),
  36.     end2;
  37.   vector<string> wordlist2;
  38.   copy(charIter, end2, back_inserter(wordlist2));
  39.   copy(wordlist2.begin(), wordlist2.end(), out);
  40.   *out++ = "-----------------------------------";
  41.   // Use a deque<char> as the source:
  42.   ifstream in2("TokenIteratorTest.cpp");
  43.   deque<char> dc;
  44.   copy(IsbIt(in2), IsbIt(), back_inserter(dc));
  45.   TokenIterator<deque<char>::iterator,Delimiters>
  46.     dcIter(dc.begin(), dc.end(), delimiters),
  47.     end3;
  48.   vector<string> wordlist3;
  49.   copy(dcIter, end3, back_inserter(wordlist3));
  50.   copy(wordlist3.begin(), wordlist3.end(), out);
  51.   *out++ = "-----------------------------------";
  52.   // Reproduce the Wordlist.cpp example:
  53.   ifstream in3("TokenIteratorTest.cpp");
  54.   TokenIterator<IsbIt, Delimiters>
  55.     wordIter2(IsbIt(in3), isbEnd, delimiters);
  56.   set<string> wordlist4;
  57.   while(wordIter2 != end)
  58.     wordlist4.insert(*wordIter2++);
  59.   copy(wordlist4.begin(), wordlist4.end(), out);
  60. } ///:~
  61.